<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
require_once (dirname ( __FILE__ ) . "/../../PHPExcel/PHPExcel.php");
class Stock extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回库存商品列表
	 */
	public function index() {
    	$name = $this->uri->segment(4);
    	$name=urldecode($name);//转化为汉字
		
    	if(empty($name)){//这里是为了将name加到url上,虽然前台也有处理，但是也有没有参数进入该方法的时候
    		$name="empty";
    	}
    	$this->load->library ( 'pagination' );
    	// 每页显示数据条数
    	$per_page=20;//每页显示20项
    	
    	$config['base_url']=site_url('stock/index/name/'.$name);
    	$this->load->model ( 'product_model' );
    	
    	if($name=="empty"){//这里是为了搜索
    		$name=null;
    	}
    	// 数据总数
    	$config ['total_rows'] = $this->product_model->count_stock_products($name);
    	//$config ['total_rows']=200; //测试用，总行数太少，分页效果无法出现
    	$config ['per_page'] = $per_page;
    	$config ['first_link'] = '第一页';
    	$config ['last_link'] = '最后一页';
    	$config ['next_link'] = '下一页';
    	$config ['prev_link'] = '上一页';
    	$config['uri_segment'] =5;
    	//$config['num_links'] = 2;
    	 
    	$this->pagination->initialize($config);
    	//echo $this->pagination->create_links();
    	$data['links']=$this->pagination->create_links();
    	//偏移量
    	$offset=intval($this->uri->segment(5));
    	
    	$data ['stocks'] = $this->product_model->find_stock_products_by_name($per_page,$offset,$name);
    	$data ['title'] = '库存商品 - 集市宝仓库管理系统';
    	$this->load->view ( 'header', $data );
    	$this->load->view ( 'stock/index' );
    	$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加库存商品页面
	 */
	public function add() {
		$data ['title'] = '添加库存商品 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'stock/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存库存商品信息
	 */
	public function save() {
		$name = $this->input->post('name');
		$content=explode('-',$name);
		$productid=$content[1];
		if(empty($productid)){
			return_json(FALSE,'','商品ID不能为空！');
		}
		$startdate = '2015-08-11';
		$enddate= date ( "Y-m-d" );
		$this->load->model ( 'cost_model' );
    	$totalproduct = $this->cost_model->get_cost_by_date_product ($productid,$startdate,$enddate);
		$this->load->model ( 'orderdetail_model' );
		$product = $this->orderdetail_model->get_orderdetail_by_date_product ($productid,$startdate,date ( "Y-m-d", strtotime ( "+1 day" )));
		$this->load->model ( 'lost_model' );
    	$lostproduct = $this->lost_model->get_lost_by_date_product ($productid,$startdate,$enddate);
		$stock=0;
		if(!empty($totalproduct)){
			if(!empty($product)){
				$totalproduct['amount']=$totalproduct['amount']-$product['amount'];
			}
			if(!empty($lostproduct)){
				$totalproduct['amount']=$totalproduct['amount']-$lostproduct['amount'];
			}
			$stock=$totalproduct['amount'];
		}
		$product = array (
				'isstock' => 1,
				'stock' => $stock
		);
		$this->load->model ( 'product_model' );
		if($this->product_model->update ( $productid, $product)){
			return_json(TRUE,'','添加成功！'); 
		}else{
			return_json(FALSE,'','添加失败！');
		}
	}
	/*
	 * 删除库存商品信息
	 */
	public function delete() {
	    $id = $this->input->post('id');
		$product = array (
				'isstock' => 0,
				'stock' => 0
		);
		$this->load->model ( 'product_model' );
		if ($this->product_model->update ( $id,$product )) {
			return_json(TRUE,'','删除成功！'); 
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
	/*
	 * 返回库存购买记录
	 */
	public function records() {
		$name = $this->uri->segment(4);
    	$name=urldecode($name);//转化为汉字
		
    	if(empty($name)){//这里是为了将name加到url上,虽然前台也有处理，但是也有没有参数进入该方法的时候
    		$name="empty";
    	}
    	$this->load->library ( 'pagination' );
    	// 每页显示数据条数
    	$per_page=20;//每页显示20项
    	
    	$config['base_url']=site_url('stock/records');
    	$this->load->model ( 'stock_model' );
		
    	if($name=="empty"){//这里是为了搜索
    		$name=null;
    	}
    	// 数据总数
    	$config ['total_rows'] = $this->stock_model->count_stocks($name);
    	//$config ['total_rows']=200; //测试用，总行数太少，分页效果无法出现
    	$config ['per_page'] = $per_page;
    	$config ['first_link'] = '第一页';
    	$config ['last_link'] = '最后一页';
    	$config ['next_link'] = '下一页';
    	$config ['prev_link'] = '上一页';
    	$config['uri_segment'] =5;
    	//$config['num_links'] = 2;
    	 
    	$this->pagination->initialize($config);
    	//echo $this->pagination->create_links();
    	$data['links']=$this->pagination->create_links();
    	//偏移量
    	$offset=intval($this->uri->segment(5));
    	
    	$data ['stocks'] = $this->stock_model->find_all_stocks($per_page,$offset,$name);
    	$data ['title'] = '库存购买记录 - 集市宝仓库管理系统';
    	$this->load->view ( 'header', $data );
    	$this->load->view ( 'stock/records' );
    	$this->load->view ( 'footer' );
	}
}